local skynet = require("skynet")
local handler = require "handler"
local util = require "util"
local reason = require "reason"
local error_code = require "error_code"
local constant = require "constant"
local cocall = require "cocall"
local act_def = require("act_def")
local mail_def = require("mail_def")

local table_insert = table.insert
local table_remove = table.remove
local table_sort = table.sort
local math_floor = math.floor

local shouchong_handler = {}

--有充值
function shouchong_handler:addRecharge(msg)
    --判断是否够100，够就改变首充状态（0没有充值达100， 1充值达100，2已发送奖励
    local uid = msg.uid
    local coins = msg.coins
    local sc_conf = global_configs.shouchong.ac_content    
    print("____addRecharge___", msg, sc_conf)
    if coins >= sc_conf.charge_target then             
        --改变状态为1
        local server = R().basesvr({key=uid})
        server:send('.msg_handler', 'update_first_charge_status', uid, 1)
    end

end

--有流水变化
function shouchong_handler:addPersonWater(msg)
    local uid = msg.uid
    local old_water = msg.old_water
    local person_water = msg.person_water
    local server = R().basesvr({key=uid})
    local sc_conf = global_configs.shouchong.ac_content
    local water_list = sc_conf.earn_coins
    local earn_num = table.size(water_list)
    local index = 0
    player.person_water2 = person_water

    local waters = {}
    for water, coins in pairs(water_list) do
        table_insert(waters, {water=water, coins=coins})
    end    
    if next(waters) then 
        table.sort(waters, function(a,b) return a.water < b.water end )
    end    
    for k, v in pairs(waters) do 
        index = index + 1
        print("___2222_________",index,old_water, person_water, v.water, v.coins)
        if person_water >= v.water and old_water < v.water then             
            --发送奖励
            local back_coins = v.coins or 0
            -- local exinfo = {channel = player.channel,cur_channel = player.cur_channel}
            -- local ok,succ,ret = server:call('.msg_handler','add_coins', uid, back_coins, reason.FIRST_CHARGE_RETRUN, exinfo)            
            -- if not ok or not succ then 
            --     errlog("___add_coins_error__", uid, msg, ok, succ, ret)
            --     return
            -- end              
            --发送邮件
            local award_list = {
                {   
                    uid = uid, 
                    title = mail_def.FIRST_CHARGE_TITLE,
                    coins = back_coins,
                    content = string.format(mail_def.FIRST_CHARGE_CONTENT, back_coins/100),
                    mail_type = 2,
                    op_user = mail_def.OP_USER,
                    reason = reason.FIRST_CHARGE_RETRUN,
                }
            }
            local url = constant.ADD_MAIL_URL
            skynet.send('.msg_handler', 'lua', 'common_post_data',url, {award_list = award_list})            
            if index == earn_num then
                local status = 2 --不能再领取奖励
                local ok, succ = server:call('.msg_handler', 'update_first_charge_status', uid, status)
                if not ok or not succ then 
                    errlog("___update_first_charge_status_error__", uid, msg, ok, succ)
                    return
                end                   
                --通知关闭首充按钮
                local data = {shouchong_open=0}
                send_to_gateway('shouchong.NTF_SHOUCHON_STATE', data)
            end
            local data = {}
            data.uid = uid
            data.type = constant.DODULE_ID.FRIST_RECHARGE  
            data.op = "partake_num"
            dbglog("_____opPartakeNum__", data)
            billlog(data)
        end
    end
end


--请求首充状态
function shouchong_handler.REQ_SHOUCHON_STATE(msg)    

    local uid = player.uid
    if not uid then
        return
    end    

    --先拉取玩家信息, 取实时金币值
    local ok, base_data = R().basesvr{key=uid}:call('.msg_handler', 'get_base_data', uid)
    if not ok then 
        return 
    end
    player.person_water2 = base_data.person_water2
    player.first_charge_status = base_data.first_charge_status


    local status = player.first_charge_status
    local person_water = player.person_water2
    if status == 0 then 
        person_water = 0
    end
    local sc_conf = global_configs.shouchong.ac_content
    print("____sc_conf___",uid, person_water, sc_conf)
    local water_list = sc_conf.earn_coins
    local target_water = 0
    local back_coins = 0
    local waters = {}
    for water, coins in pairs(water_list) do
        table_insert(waters, {water=water, coins=coins})
    end  
    if next(waters) then 
        table.sort(waters, function(a,b) return a.water < b.water end )
    end
    for k, v in pairs(waters) do 
        if back_coins == 0 and person_water < v.water then 
            target_water = v.water
            back_coins = v.coins            
        end
    end

    local res = {charge_activity = {
        charge_state = status, --状态（0没有充值达100， 1充值达100，2已发送奖励
        cur_water = person_water, --当前流水
        target_water = target_water, --目标流水
        award_coins = back_coins, --返金币
        target_charge = sc_conf.charge_target, --首充目标  
        water_list = waters,
        -- bt_status = 0,      
    }}
    dbglog("___shouchong_getSCState_res____", res)

    send_to_gateway('shouchong.RSP_SHOUCHON_STATE',res)

    return true
end



--------------------------------------------------------
handler.register_module_handlers('shouchong', shouchong_handler)

